<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2019/12/16
 * Time: 15:33
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Cartype extends Base
{
    /**
     * 车辆类别列表
     * @return mixed|\think\response\Json
     */
    public function typeIndex(){
        if(request()->isAjax()){
            $c_type_name = input('c_type_name'); // 名称
            $c_type_show = input('c_type_show'); // 状态
            $where = [];
            if(isset($c_type_show) && $c_type_show != ""){
                $where['c_type_show'] = $c_type_show;
            }
            if(isset($c_type_name) && $c_type_name != ''){
                $where['c_type_name'] = ['like', '%' . $c_type_name . '%'];
            }
            
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('car_type')->where($where)->count();// 获取总条数
            $lists = Db::name('car_type')
                ->where($where)
                ->field('c_type_id, c_type_name, c_type_show, c_type_time')
                ->order('c_type_time desc')
                ->page($NowPage, $limits)
                ->select();
            foreach($lists as $k => $v){
                $car_param = Db::table('up_car_param')
                    ->where('c_param_fid', $v['c_type_id'])
                    ->count();
                $lists[$k]['car_param'] = $car_param;
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch('cartype/typeindex');
    }
    
    /**
     * 添加类别
     * @return mixed|\think\response\Json
     */
    public function addCarType(){
        if(Request::instance()->isPost()){
            $c_type_name = trim(input('c_type_name'), ' ');
            $c_type_show = input('c_type_show');
            $have = Db::table('up_car_type')->where('c_type_name', $c_type_name)->find();
            if($have){
                return json(['code' => 100, 'msg' => '已有该类别']);
            }
            $insert = [
                'c_type_name' => $c_type_name,
                'c_type_show' => $c_type_show,
                'c_type_time' => date('Y-m-d H:i:s'),
                'c_type_update_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_car_type')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch('cartype/addcartype');
    }
    
    /**
     * 编辑类别
     * @return mixed|\think\response\Json
     */
    public function carTypeEdit(){
        $c_type_id = input('c_type_id');
        if(Request::instance()->isPost()){
            $c_type_name = trim(input('c_type_name'), ' ');
            $c_type_show = input('c_type_show');
            $have = Db::table('up_car_type')
                ->where('c_type_name', $c_type_name)
                ->where('c_type_id', 'NEQ', $c_type_id)
                ->find();
            if($have){
                return json(['code' => 100, 'msg' => '已有该类别']);
            }
            $update = [
                'c_type_name' => $c_type_name,
                'c_type_show' => $c_type_show,
                'c_type_update_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_car_type')->where('c_type_id', $c_type_id)->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $info = Db::table('up_car_type')
            ->field('c_type_id, c_type_name, c_type_show')
            ->where('c_type_id', $c_type_id)
            ->find();
        $this->assign('info', $info);
        $this->assign('c_type_id', $c_type_id);
        return $this->fetch('cartype/cartypeedit');
    }
    
    /**
     * 状态
     * @return \think\response\Json
     */
    public function carTypeStatus(){
        $c_type_id = input('id');
        $status = Db::table('up_car_type')->where('c_type_id', $c_type_id)->value('c_type_show');
        if($status == 1){
            $c_type_show = 2;
        }else{
            $c_type_show = 1;
        }
        $res = Db::table('up_car_type')->where('c_type_id', $c_type_id)->setField('c_type_show', $c_type_show);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 删除类别
     * @return \think\response\Json
     */
    public function carTypeDel(){
        $c_car_id = input('id');
        $have = Db::table('up_car_type_param')->where('ctp_type_id', $c_car_id)->find();
        if($have){
            return json(['code' => 100, 'msg' => '有车辆拥有该类别，不可删除']);
        }
        $res = Db::table('up_car_type')->delete($c_car_id);
        if($res){
            Db::table('up_car_param')->where('c_param_fid', $c_car_id)->delete();
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
    
    /**
     * 属性列表
     * @return mixed|\think\response\Json
     */
    public function paramIndex(){
        $c_type_id = input('c_type_id');
        if($c_type_id != ''){
            session('c_type_id', $c_type_id);
        }
        if(request()->isAjax()){
            $c_param_name = input('c_param_name'); // 名称
            $c_param_show = input('c_param_show'); // 状态
            $start_time = input('start_time'); // 开始时间
            $end_time = input('end_time'); // 结束时间
            $where['c_param_fid'] = $c_type_id;
            if(isset($c_param_show) && $c_param_show != ""){
                $where['c_param_show'] = $c_param_show;
            }
            if(isset($c_param_name) && $c_param_name != ''){
                $where['c_param_name'] = ['like', '%' . $c_param_name . '%'];
            }
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time != ''){
                $where['c_param_time'] = ['between', [$start_time, $end_time]];
            }
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time == ''){
                $where['c_param_time'] = ['> time', $start_time];
            }
            if(isset($start_time) && $start_time == '' && isset($end_time) && $end_time != ''){
                $where['c_param_time'] = ['< time', $end_time];
            }
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('car_param')->where($where)->count();// 获取总条数
            $lists = Db::name('car_param')
                ->where($where)
                ->field('c_param_id, c_param_name, c_param_show, c_param_time')
                ->order('c_param_time desc')
                ->page($NowPage, $limits)
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch('cartype/paramindex');
    }
    
    /**
     * 添加属性
     * @return mixed|\think\response\Json
     */
    public function addCarParam(){
        if(Request::instance()->isPost()){
            $c_param_name = trim(input('c_param_name'), ' ');
            $c_param_show = input('c_param_show');
            $have = Db::table('up_car_param')->where('c_param_name', $c_param_name)->find();
            if($have){
                return json(['code' => 100, 'msg' => '已有该类别']);
            }
            $insert = [
                'c_param_name' => $c_param_name,
                'c_param_fid' => session('c_type_id'),
                'c_param_show' => $c_param_show,
                'c_param_time' => date('Y-m-d H:i:s'),
                'c_param_update_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_car_param')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch('cartype/addcarparam');
    }
    
    /**
     * 编辑属性
     * @return mixed|\think\response\Json
     */
    public function carParamEdit(){
        $c_param_id = input('c_param_id');
        if(Request::instance()->isPost()){
            $c_param_name = trim(input('c_param_name'), ' ');
            $c_param_show = input('c_param_show');
            $have = Db::table('up_car_param')
                ->where('c_param_name', $c_param_name)
                ->where('c_param_id', 'NEQ', $c_param_id)
                ->find();
            if($have){
                return json(['code' => 100, 'msg' => '已有该类别']);
            }
            $update = [
                'c_param_name' => $c_param_name,
                'c_param_show' => $c_param_show,
                'c_param_update_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_car_param')->where('c_param_id', $c_param_id)->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $info = Db::table('up_car_param')
            ->field('c_param_id, c_param_name, c_param_show')
            ->where('c_param_id', $c_param_id)
            ->find();
        $this->assign('info', $info);
        $this->assign('c_param_id', $c_param_id);
        return $this->fetch('cartype/carparamedit');
    }
    
    /**
     * 状态
     * @return \think\response\Json
     */
    public function carParamStatus(){
        $c_param_id = input('id');
        $status = Db::table('up_car_param')->where('c_param_id', $c_param_id)->value('c_param_show');
        if($status == 1){
            $c_param_status = 2;
        }else{
            $c_param_status = 1;
        }
        $res = Db::table('up_car_param')->where('c_param_id', $c_param_id)->setField('c_param_show', $c_param_status);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 删除属性
     * @return \think\response\Json
     */
    public function carParamDel(){
        $c_param_id = input('id');
        $have = Db::table('up_car_type_param')->where('ctp_param_id', $c_param_id)->find();
        if($have){
            return json(['code' => 100, 'msg' => '有车辆拥有该属性，不可删除']);
        }
        $res = Db::table('up_car_param')->delete($c_param_id);
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
}